﻿//-----------------------------------------------------------------------
// <copyright file="SimpleFileLogger.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------

using System;
using System.IO;

namespace ConsoleHoster.Common.Utilities
{
    public sealed class SimpleFileLogger : IDisposable, ILogger
    {
        private Object syncObject = new Object();

        private bool disposed = false;

        private StreamWriter logFile;

        public SimpleFileLogger(string argLogFile)
        {
            this.Initialize(argLogFile);
        }

        private void Initialize(string logPath)
        {
            try
            {
                this.logFile = new StreamWriter(logPath, true);
                this.LogFile.AutoFlush = true;
            }
            catch (Exception ex)
            {
                if (this.logFile != null)
                {
                    try
                    {
                        this.logFile.Close();
                        this.logFile.Dispose();
                    }
                    catch
                    {
                    }
                }
                throw new Exception("Unable to initialize the logger", ex);
            }
        }

        public void LogError(string argFormat, params string[] args)
        {
            this.LogMessage("ERROR: {0}", StringUtilities.Format(argFormat, args));
        }

        public void LogMessage(string argFormat, params string[] args)
        {
            lock (this.syncObject)
            {
                DateTime now = DateTime.Now;
                StreamWriter tmpWriterReference = this.LogFile;
                if (tmpWriterReference != null)
                {
                    tmpWriterReference.WriteLine("{0}:\t{1}", now.ToString("ddd, MMM dd, yyyy HH:mm:ss.fff"), StringUtilities.Format(argFormat, args));
                }
            }
        }

        public void LogWarning(string argFormat, params string[] args)
        {
            this.LogMessage("WARNING: {0}", StringUtilities.Format(argFormat, args));
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        private void Dispose(bool argDisposing)
        {
            if (!this.disposed)
            {
                lock (this.syncObject)
                {
                    if (!this.disposed)
                    {
                        if (argDisposing)
                        {
                            if (this.LogFile != null)
                            {
                                this.LogFile.Dispose();
                                this.LogFile = null;
                            }
                        }
                    }
                }

                this.disposed = true;
            }
        }

        private StreamWriter LogFile
        {
            get
            {
                return this.logFile;
            }
            set
            {
                this.logFile = value;
            }
        }
    }
}